/**
 * 不含税单价（总共保留12位）
 */
export function toFixExclTaxPrice(price) {
  let exclTaxPrice = parseFloat(price + '')
  if (price.toString().length >= 13) {
    exclTaxPrice = price.toString().substr(0, 13)
  }
  return exclTaxPrice
}

/**
 * 通过含税单价算出不含税单价
 */
export function exclTaxPrice(price, taxRate) {
  const myPrice = divide(price, plus(1, taxRate))
  const myExclTaxPrice = toFixExclTaxPrice(myPrice)
  return myExclTaxPrice
}

/**
 * 通过总金额算税额
 */
export function calculateTax(amount, taxRate) {
  const tax = parseFloat(times(divide(amount, plus(1, taxRate)), taxRate).toFixed(2))
  return tax
}

/**
 * 通过总金额算不含税金额
 */
export function calculateExclTaxAmount(amount, taxRate) {
  const tax = parseFloat(times(divide(amount, plus(1, taxRate)), taxRate).toFixed(2))
  return minus(amount, tax)
}

/**
 * 通过含税单价算出税额
 */
export function taxRatePrice(num, price, taxRate) {
  const RatePrice = times(divide(times(num, price), plus(1, taxRate)), taxRate)
  const myRatePrice = parseFloat(RatePrice.toFixed(2))
  return myRatePrice
}

/**
 * 通过含税单价和数量算出总金额
 */
export function allMoney(num, price) {
  const allPrice = times(num, price)
  const money = parseFloat(allPrice.toFixed(2))
  return money
}

/**
 * 借方-期初余额
 */
export function debitBlance(blance, debit, credit) {
  const myBlance = minus(plus(blance, debit), credit)
  const newBlance = parseFloat(myBlance.toFixed(2))
  return newBlance
}

/**
 * 贷方-期初余额
 */
export function creditBlance(blance, debit, credit) {
  const myBlance = minus(plus(blance, credit), debit)
  const newBlance = parseFloat(myBlance.toFixed(2))
  return newBlance
}

/**
 * 退货数量
 */
export function retunum(sum, rem, edit) {
  const max = minus((sum || 0), (rem || 0))
  const num = minus((sum || 0), plus((rem || 0), (edit || 0)))
  if (num >= 0) {
    return 'ok'
  } else {
    return max
  }
}

export function handleBillRowData(rowData, taxRate) {
  taxRate = Number(taxRate)
  rowData.num = Number(rowData.num)
  rowData.price = Number(rowData.price)
  rowData.exclTaxPrice = exclTaxPrice(rowData.price, taxRate)
  rowData.originalPrice = rowData.originalPrice ? rowData.originalPrice : rowData.price
  rowData.tax = taxRatePrice(rowData.num, rowData.price, taxRate)
  rowData.taxRate = taxRate
  rowData.amount = allMoney(rowData.num, rowData.price)
  rowData.exclTaxAmount = minus(rowData.amount, rowData.tax)
  rowData.originalAmount = allMoney(rowData.num, rowData.originalPrice)
  return rowData
}

export function count(dataList, sortNum = true) {
  const total = { num: 0, amount: 0, tax: 0, originalAmount: 0, exclTaxAmount: 0 }
  if (!dataList || dataList.length === 0) {
    return total
  } else {
    dataList.forEach(function(item, index) {
      sortNum ? item.sortNum = index : ''
      total.num = plus(total.num, Number(item.num))
      total.amount = plus(total.amount, Number(item.amount))
      total.tax = plus(total.tax, Number(item.tax))
      total.exclTaxAmount = plus(total.exclTaxAmount, Number(item.exclTaxAmount))
      total.originalAmount = plus(total.originalAmount, Number(item.originalAmount))
    })
  }
  return total
}

// 加
export function plus(num1, num2) {
  if (num1 === null || num1 === undefined) {
    num1 = 0
  }
  if (num2 === null || num2 === undefined) {
    num2 = 0
  }
  const num1Digits = (num1.toString().split('.')[1] || '').length
  const num2Digits = (num2.toString().split('.')[1] || '').length
  const baseNum = Math.pow(10, Math.max(num1Digits, num2Digits))
  return (times(num1, baseNum) + times(num2, baseNum)) / baseNum
}

export function toArrayValue(str, map) {
  const array = []
  str.split(',').forEach((item) => {
    if (map[item]) {
      array.push(map[item])
    }
  })
  return array
}

// 累加
export function plusAll(array) {
  let total = 0
  if (array) {
    array.forEach(item => {
      total = plus(total, item)
    })
  }
  return total
}

// 减
export const minus = (num1, num2) => {
  if (num1 === null || num1 === undefined) {
    num1 = 0
  }
  if (num2 === null || num2 === undefined) {
    num2 = 0
  }
  const num1Digits = (num1.toString().split('.')[1] || '').length
  const num2Digits = (num2.toString().split('.')[1] || '').length
  const baseNum = Math.pow(10, Math.max(num1Digits, num2Digits))
  return (times(num1, baseNum) - times(num2, baseNum)) / baseNum
}

// 保留位数
export function getScale(num, scale) {
  if (num === null || num === undefined || num === '') {
    return ''
  }
  return parseFloat(num).toFixed(scale)
}

// 乘
export function times(num1, num2) {
  if (num1 === null || num1 === undefined || num1 === '') {
    num1 = 0
  }
  if (num2 === null || num2 === undefined || num2 === '') {
    num2 = 0
  }
  const num1String = num1.toString()
  const num2String = num2.toString()
  const num1Digits = (num1String.split('.')[1] || '').length
  const num2Digits = (num2String.split('.')[1] || '').length
  const baseNum = Math.pow(10, num1Digits + num2Digits)
  return Number(num1String.replace('.', '')) * Number(num2String.replace('.', '')) / baseNum
}

// 乘 (保留位数）
export function timesScale(num1, num2, scale) {
  return parseFloat(times(num1, num2).toFixed(scale))
}

// 除
export function divide(num1, num2) {
  if (num1 === null || num1 === undefined || num1 === '') {
    num1 = 0
  }
  if (num2 === null || num2 === undefined || num2 === '' || num2 === 0) {
    num2 = 1
  }
  const num1String = num1.toString()
  const num2String = num2.toString()
  const num1Digits = (num1String.split('.')[1] || '').length
  const num2Digits = (num2String.split('.')[1] || '').length
  const baseNum = Math.pow(10, num1Digits + num2Digits)
  const n1 = times(num1, baseNum)
  const n2 = times(num2, baseNum)
  return Number(n1) / Number(n2)
}

// 除 (保留位数）
export function divideScale(num1, num2, scale) {
  return parseFloat(divide(num1, num2).toFixed(scale))
}

// 每三位数字逗号分隔数字
export function NumFormat(value) {
  if (!value) return ''
  const num = parseFloat(value)
  var intPart = Math.trunc(num)// 获取整数部分
  var intPartFormat = intPart.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') // 将整数部分逢三一断
  if (num < 0 && num > -1) {
    intPartFormat = '-0'
  }
  const array = value.toString().split('.')
  if (array.length > 1) {
    const floatPart = array[1].toString() // 拿到小数部分
    return intPartFormat + '.' + floatPart
  } else {
    return intPartFormat
  }
}
